using Gtk;
using GLib;

namespace Atlantis
{
    public class Document : SourceBuffer
    {
        private string fileName;
	    private string baseName;
    
        public Document(string path) 
        {
            this.fileName = uri_to_filename(path);
            this.baseName = Path.get_basename(this.fileName);
            SourceLanguageManager slm;
            SourceLanguage lang;

            slm = new SourceLanguageManager();
            lang = slm.get_language("vala");

            if ( lang != null ) 
            {
                string content;
                FileUtils.get_contents (fileName, out content, null);
                this.text = content;
                set_language (lang);
                set_highlight_syntax(true);
            }
        }
        
        public string FullPath
        {
			get
			{
				return this.fileName;
			}
		}

        public string Name
        {
            get
            {
                return this.baseName;
            }
        }

        public string uri_to_filename (string uri) 
        {
            string f = uri;
    
            // only local URIs are supported for now
            if ( uri.has_prefix ("file:") )
                f = uri.split ("://")[1];
            else if ( ! Path.is_absolute (uri) )
                f = Path.build_filename (Environment.get_current_dir (), uri);
            
            return f;
        }
    }
}
